#include <plat.h>
#include <pl011_uart.h>
#include <spinlock.h>

Pl011_Uart *uart  = (void*)UART_BASE;

spinlock_t print_lock = SPINLOCK_INITVAL;

void console_init(void)
{
    pl011_uart_init(uart);
    pl011_uart_enable(uart);

    return;
}

void console_putc(char c)
{
    pl011_uart_putc(uart, c);
}

void console_puts(const char *ptr, int len)
{
    int i;

    for (i = 0; i < len; ++i)
    {
        if (ptr[i] == '\n')
        {
            console_putc('\r');
        }
        console_putc(ptr[i]);
    }
}

char console_getchar(void)
{
    return pl011_uart_getc(uart);
}

void console_clear_rxirq(){
    while(!(uart->flag & UART_FR_RXFE)) {
        volatile char c = uart->data;
    }
    uart->isr_clear = 0xffff;
}

